<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-wms__locate}">
      <wms-form 
        v-model="dataForm"
        :formList="formList"
        :table-column="tableColumn"
        :advanced="advanced"
        @check-change="checkChange"
        @changeAdvance="changeAdvance"
        @getDataList="getDataList"
        @getReset="getReset"
      >
        <el-form-item>
          <el-button type="danger" plain @click="disables('disable')">一键禁用
          </el-button>
          <el-button type="success" plain @click="disables('enable')">一键解禁
          </el-button>
        </el-form-item>
      </wms-form>
      <wms-table
        ref="wmsTable"
        v-model="dataForm"
        :page="page"
        :total="total"
        :limit="limit"
        :tableData="dataList"
        :table-column="column"
        :module-options="mixinViewModuleOptions"
        :dataListLoading="dataListLoading"
        :showSelection="true"
        @selection-change="handleSelectionChange"
        @changeStation="changeStation"
        @pageSizeChangeHandle="pageSizeChangeHandle"
        @pageCurrentChangeHandle="pageCurrentChangeHandle"/>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./components/locate-add-or-update";
import debounce from "lodash/debounce";
import { formList, tableColumn } from './config'
import wmsTable from '@/components/wms-table'
import wmsForm from '@/components/wms-form'
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/wms/locate/page",
        getDataListIsPage: true,
        deleteURL: "/wms/locate",
        deleteIsBatch: true,
      },
      dataForm: {
        
      },
      selectedIds: [],
      formList,
      tableColumn,
      column: [],
      advanced: false
    };
  },
  components: {
    AddOrUpdate,
    wmsTable,
    wmsForm
  },
  methods: {
    //0.重置搜索框
    getReset() {
      this.dataForm = this.$options.data().dataForm;
    },
    // 处理表格多选
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.locateId)
    },
    // 一键禁用操作
    batchHandle(statusType) {
      console.log(this.selectedIds)
      if (this.selectedIds.length === 0) {
        return this.$message.warning('请至少选择一条数据')
      }
      this.$confirm(`确定要${statusType ? '解禁' : '禁用'}选中库位吗？`, '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const { data: res } = await this.$http.post('/wms/locate/disables', {
            ids: this.selectedIds,
            status: statusType
          })
          console.log(res)
          if (res.code !== 0) return this.$message.error(res.msg)
          this.$message.success(`${statusType ? '解禁' : '禁用'}成功`)
          this.getDataList() // 刷新数据
        } catch (err) {
          console.error('操作失败:', err)
        }
      })
    },
    // 修改按钮点击事件
    disables(type) {
      const statusType = type === 'enable' ? 1 : 0;  // 转换为状态值
      this.batchHandle(statusType);  // 传递参数
    },
    //修改库位状态
    changeStation: debounce(function (val, row) {
      var tempStatus = row.active === 0 ? 1 : 0;
      this.dataForm.locateId = row.locateId;
      this.$http.post(`/wms/locate/forbid/${row.locateId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          //如果更新失败则重置为原来的状态
          row.active = tempStatus;
          return this.$message.error(res.msg)
        }
        if (tempStatus !== 1) {
          console.info("1111");
          this.$message({
            message: '货位已禁用',
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }
        if (tempStatus !== 0) {
          console.info("0000");
          this.$message({
            message: '货位解禁成功',
            type: 'success',
            duration: 500,
            onClose: () => {
            }
          })
        }
      }).catch(() => { })
    }),
    frobidHandle(id) {
      if (!this.dataForm.locateId) {
        return this.$message({
          message: this.$t('prompt.forbid'),
          type: 'warning',
          duration: 500
        })
      }
      this.$confirm(this.$t('prompt.info', { 'handle': this.$t('forbid') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.$http.post('/wms/locate/forbid/' + this.dataForm.locateId)
          .then(({ data: res }) => {
            if (res.code != 0) {
              return this.$message.error(res.msg)
            }
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 500,
              onClose: () => {
                this.getDataList()
              }
            })
          }).catch(() => { })


      }).catch(() => { })

    }
  },
};
</script>
<style>
.right-handle-group {
  float: right;
}
</style>


